// simple dummy program does some work, exits with a random chance, does I/O with a random chance and dummy syscalls

// jump over the variables sections
jmp init

// define external the variables (just space reservations)
rand:	.val 0
iochance:
		.val 0
dummychance:
		.val 0
exitchance:
		.val 0
iobursthi:
		.val 0
ioburstlo:
		.val 0
// define internal variables
iotime:	.val 0
nextint:
		.val 0
action: .val 0
i:		.val 0
num:	.val 0

// initialize the variables		
init:

mov rand, 99999
mov exitchance, 40

mov ioburstlo, 15
mov iobursthi, 200

// io_chance = r.nextInt(300) + 300
mov r1, 300
sop 1
add r8, 1800
mov iochance, r8

// dummy_chance = r.nextInt(300) + 500
mov r1, 900
sop 1
add r8, 500
mov dummychance, r8

mov r1, 2000
sop 1
mov num, r8
add num, 1000


jmp start

// the actual program


start:

// //decide what to do this iteration
// int nextInt = r.nextInt(rand);
mov r1, rand
sop 1
mov nextint, r8

// int action = io_chance;
mov action, iochance

// if (nextInt < io_chance) {
jge nextint, iochance, condition2
 
//	// remember a syscall can fail (important for io_wait)
//	int io_time = r.nextInt(io_burst_hi - io_burst_lo) + io_burst_lo;
mov r1, iobursthi
sub r1, ioburstlo
sop 1
add r8, ioburstlo
mov iotime, r8

//	Syscall.do_IO_req(this, io_time);
mov r0, 1
mov r1, iotime
int 120

//	return;
jmp end

// }
condition2:
// if (nextInt < (action += dummy_chance)) {
add action, dummychance
jge nextint, action, condition3

//	// just du syscalls like a "normal" "real" process
//	Syscall.do_dummy(this);
mov r0, 2
int 120

//	return;
jmp end

// }
condition3:
// if (nextInt < (action += exit_chance)) {
add action, exitchance
jge nextint, action, default

//	Syscall.do_exit(this);
mov r0, 3
int 120

//	return;
jmp end

//}
default:
//for (int i = 0; i < 10000; i++)
mov i, 0



loop:
jge i, num, end_loop

mov r1, 65535
sop 3

add i, 1
jmp loop

end_loop:
jmp end

end:
jmp start
